package week4;


public class Ex35 {
    public static void main(String[] args) {
        int m = 1000000;
        int[] prime = new int[m];
        int c = 0;
        boolean[] isVisit = new boolean[m+1];
        for (int i = 2; i <= m; ++i){
            if (!isVisit[i])
                prime[++c] = i;
            for (int j = 1; j <= c && i * prime[j] <= m; ++j){
                isVisit[i*prime[j]] = true;
                if (i % prime[j] == 0)
                    break;
            }
        }
        isVisit[0] = isVisit[1] = true;
        int ans = 13;
        char[] arr;
        String temp;
        boolean flag;
        for (int i = 100; i < m; ++i){
            arr = String.valueOf(i).toCharArray();
            flag = true;
            for (int j = 0; j <= arr.length - 1; ++j){
                temp = "";
                for (int k = 0; k <= arr.length - 1; ++k)
                    temp += arr[(j+k)%arr.length];
                if (isVisit[Integer.parseInt(temp)]){
                    flag = false;
                    break;
                }
            }
            if (flag)
                ans++;
        }
        System.out.println(ans);
    }
}
